package com.company.oa.announcement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.oa.announcement.entity.AnnouncementAttachment;
import com.company.oa.announcement.mapper.AnnouncementAttachmentMapper;
import com.company.oa.announcement.service.AnnouncementAttachmentService;
import com.company.oa.common.exception.BusinessException;
import com.company.oa.common.util.FileUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AnnouncementAttachmentServiceImpl extends ServiceImpl<AnnouncementAttachmentMapper, AnnouncementAttachment>
        implements AnnouncementAttachmentService {

    @Value("${file.upload.path}")
    private String uploadPath;

    @Value("${file.upload.max-size}")
    private long maxFileSize;

    @Override
    public Long uploadAttachment(MultipartFile file, Long announcementId, Long uploaderId, String uploaderName) {
        // 检查文件大小
        if (file.getSize() > maxFileSize) {
            throw new BusinessException("文件大小超过限制，最大支持" + (maxFileSize / 1024 / 1024) + "MB");
        }

        try {
            // 上传文件
            String originalFilename = file.getOriginalFilename();
            String fileType = FileUtil.getFileExtension(originalFilename);
            String filePath = FileUtil.uploadFile(file, uploadPath + "/announcement");

            // 保存附件信息
            AnnouncementAttachment attachment = new AnnouncementAttachment();
            attachment.setAnnouncementId(announcementId);
            attachment.setName(originalFilename);
            attachment.setPath(filePath);
            attachment.setSize(file.getSize());
            attachment.setType(fileType);
            attachment.setUploaderId(uploaderId);
            attachment.setUploaderName(uploaderName);
            attachment.setUploadTime(LocalDateTime.now());
            attachment.setIsDeleted(0);

            save(attachment);

            return attachment.getId();
        } catch (IOException e) {
            throw new BusinessException("文件上传失败: " + e.getMessage());
        }
    }

    @Override
    public List<AnnouncementAttachment> getByAnnouncementId(Long announcementId) {
        LambdaQueryWrapper<AnnouncementAttachment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AnnouncementAttachment::getAnnouncementId, announcementId)
                .eq(AnnouncementAttachment::getIsDeleted, 0)
                .orderByDesc(AnnouncementAttachment::getUploadTime);

        return list(wrapper);
    }

    @Override
    public boolean deleteAttachment(Long id) {
        AnnouncementAttachment attachment = getById(id);
        if (attachment == null) {
            throw new BusinessException("附件不存在");
        }

        // 逻辑删除
        attachment.setIsDeleted(1);
        return updateById(attachment);
    }
}